{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let's run a simple image classifier, using the CIFAR 10 dataset of 10 image categories\n",
    "* CIFAR's 10 categories:\n",
    " * airplane\n",
    " * automobile\n",
    " * bird\n",
    " * cat\n",
    " * deer\n",
    " * dog\n",
    " * frog\n",
    " * horse\n",
    " * ship\n",
    " * truck"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "from keras.models import load_model\n",
    "from keras.datasets import cifar10 \n",
    "\n",
    "img_row, img_height, img_depth = 32,32,3\n",
    "\n",
    "classifier = load_model('/home/deeplearningcv/DeepLearningCV/Trained Models/cifar_simple_cnn.h5')\n",
    "\n",
    "# Loads the CIFAR dataset\n",
    "(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n",
    "color = True \n",
    "scale = 8\n",
    "\n",
    "def draw_test(name, res, input_im, scale, img_row, img_height):\n",
    "    BLACK = [0,0,0]\n",
    "    res = int(res)\n",
    "    if res == 0:\n",
    "        pred = \"airplane\"\n",
    "    if res == 1:\n",
    "        pred = \"automobile\"\n",
    "    if res == 2:\n",
    "        pred = \"bird\"\n",
    "    if res == 3:\n",
    "        pred = \"cat\"\n",
    "    if res == 4:\n",
    "        pred = \"deer\"\n",
    "    if res == 5:\n",
    "        pred = \"dog\"\n",
    "    if res == 6:\n",
    "        pred = \"frog\"\n",
    "    if res == 7:\n",
    "        pred = \"horse\"\n",
    "    if res == 8:\n",
    "        pred = \"ship\"\n",
    "    if res == 9:\n",
    "        pred = \"truck\"\n",
    "        \n",
    "    expanded_image = cv2.copyMakeBorder(input_im, 0, 0, 0, imageL.shape[0]*2 ,cv2.BORDER_CONSTANT,value=BLACK)\n",
    "    if color == False:\n",
    "        expanded_image = cv2.cvtColor(expanded_image, cv2.COLOR_GRAY2BGR)\n",
    "    cv2.putText(expanded_image, str(pred), (300, 80) , cv2.FONT_HERSHEY_COMPLEX_SMALL,4, (0,255,0), 2)\n",
    "    cv2.imshow(name, expanded_image)\n",
    "\n",
    "\n",
    "for i in range(0,10):\n",
    "    rand = np.random.randint(0,len(x_test))\n",
    "    input_im = x_test[rand]\n",
    "    imageL = cv2.resize(input_im, None, fx=scale, fy=scale, interpolation = cv2.INTER_CUBIC) \n",
    "    input_im = input_im.reshape(1,img_row, img_height, img_depth) \n",
    "    \n",
    "    ## Get Prediction\n",
    "    res = str(classifier.predict_classes(input_im, 1, verbose = 0)[0])\n",
    "    \n",
    "    draw_test(\"Prediction\", res, imageL, scale, img_row, img_height) \n",
    "    cv2.waitKey(0)\n",
    "\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
